package com.lms.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.lms.dao.lmsDAOImpl;
import com.lms.domain.LoginDetails;
import com.lms.domain.NewProjects;
import com.lms.domain.User;
import com.lms.dto.ProjectMapper;
import com.lms.dto.UserDto;
import com.lms.dto.UserMapper;
import com.lms.service.ProjectService;



@Controller
public class AppController extends MultiActionController {
	@Autowired
	private com.lms.dao.lmsDAO lmsDAO;
	
	@Autowired
	private com.lms.service.UserService service;
	
	@Autowired
	private ProjectService projectService;
	

	public void setLmsDAO(lmsDAOImpl lmsDAO) {
		this.lmsDAO = lmsDAO;
	}

	@RequestMapping("/login.html")
	public ModelAndView Welcome(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Login login = new Login();
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute(login);

		ModelAndView b = new ModelAndView("login");
		b.addAllObjects(modelMap);
		return b;
	}

	@RequestMapping("/home.html")
	public ModelAndView MainPage(HttpServletRequest request,
			HttpServletResponse response, Login login) throws Exception {
		HttpSession session = request.getSession();
		
		ModelMap modelMap = new ModelMap();
		ModelAndView b = new ModelAndView("home");
		b.addAllObjects(modelMap);
		return b;
	/*	if (session != null) {
			ModelMap modelMap = new ModelMap();
			List<Login> loginDetails = lmsDAO.getLogin(login);
			session.setAttribute("login", login);
			if (!loginDetails.isEmpty()) {
					
				ModelAndView b = new ModelAndView("home");
				b.addAllObjects(modelMap);
				return b;
			} else {
				return new ModelAndView("redirect:login.html");
			}
		}
		return new ModelAndView("error");*/
	}
	
	

	@RequestMapping("/logout.html")
	public ModelAndView Logout(HttpServletRequest request,
			HttpServletResponse response) {

		if (request.getSession() != null) {
			request.getSession().invalidate();
			return new ModelAndView("logout");
		}
		return new ModelAndView("redirect:error.html");

	}

	@RequestMapping("/error.html")
	public ModelAndView error(HttpServletRequest request,
			HttpServletResponse response) {

		return new ModelAndView("error");

	}

	@RequestMapping("/ChangePassword.html")
	public ModelAndView ChangePassword(HttpServletRequest request,
			HttpServletResponse response) {

		HttpSession session = request.getSession();
		if (session != null) {
			System.out.println("Change Password Page!");

			LoginDetails changePass = new LoginDetails();
			ModelAndView b = new ModelAndView();
			b.addObject("changePass", changePass);
			return b;
		}
		return new ModelAndView("redirect:error.html");
	}

	@RequestMapping("/ConfirmChangePass.html")
	public ModelAndView ConfirmChangePass(HttpServletRequest request,
			HttpServletResponse response, LoginDetails newPass) {

		HttpSession session = request.getSession();
		if (session != null) {
			System.out.println("Confirm Change Password Page!");
			newPass.setEmployeeId(((Login) session.getAttribute("login"))
					.getEmployeeId());
			lmsDAO.changePassword(newPass);
			return new ModelAndView("redirect:Welcome.html");
		}
		return new ModelAndView("redirect:error.html");
	}
	
		
	@RequestMapping(value="/recordsSearch.html")
	public @ResponseBody Object getSearchUsers(
			@RequestParam String criteria,
			@RequestParam String username) {
		System.out.println("getSearchUsers.....");

		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		System.out.println(criteria);
		System.out.println("\n\n\n\n\n\n\n\n\n\n\n");
		try {
			if(criteria.equalsIgnoreCase("employeeName")) {
				User user = new User();
				user.setUsername(username);
				return UserMapper.map(service.readEmployeeSearch(user));
			}
			else{
				NewProjects project = new NewProjects();
				project.setProjectId(username);
				return ProjectMapper.mapProject(projectService.readProjectSearch(project));

			}
		}
		catch(Exception e) {
			return null;
		}

				}
	@RequestMapping("/searchResults.html")
	public ModelAndView searchResults(HttpServletRequest request,
			HttpServletResponse response) {

		return new ModelAndView("searchResults");

	}
	
	
	
}
